package zut.cs.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import zut.cs.dao.StudentDao;
import zut.cs.daomain.Student;

import java.util.List;

@Service("studentService")
public class StudentService {

    @Autowired
    StudentDao studentDao;

    @Transactional
    public List<Student> findAll(){
        return studentDao.findAll();
    }

    @Transactional
    public void stuAVG(){
        float sum=0,i=0;
        List<Student> stuList = studentDao.findAll();
        for(Student s : stuList){
            sum=s.getResult()+sum;
            i++;
        }
        float avg = sum/i;
        System.out.println(avg);
    }

    @Transactional
    public void insert(Student student){
        studentDao.insert(student);
    }

    @Transactional
    public void levelPart(){
        List<Student> stuList = studentDao.findAll();
        for(Student s : stuList){
            if(s.getResult()<60)
                s.setLevel("E");
            else if(s.getResult()>=60&&s.getResult()<70)
                s.setLevel("D");
            else if(s.getResult()>=70&&s.getResult()<80)
                s.setLevel("C");
            else if(s.getResult()>=80&&s.getResult()<90)
                s.setLevel("B");
            else if(s.getResult()>=90&&s.getResult()<=100)
                s.setLevel("A");
            studentDao.modifyLevel(s);
        }

    }
}
